package com.ilop.sthome.mvp.present.device;

import android.content.Context;
import android.os.Handler;

import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.example.common.mvp.onModelCallBack;
import com.example.common.mvp.BasePresenterImpl;
import com.example.xmpic.ui.utils.CameraDaoUtil;
import com.ilop.sthome.common.command.SendDeviceCommand;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.DeviceRenameContract;
import com.example.xmpic.mvp.model.CameraModel;
import com.ilop.sthome.mvp.model.CommonModel;
import com.example.xmpic.mvp.model.CameraModelImpl;
import com.ilop.sthome.mvp.model.common.CommonModelImpl;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.tools.ByteUtil;

/**
 * @author skygge
 * @date 2020/7/15.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class DeviceRenamePresenter extends BasePresenterImpl<DeviceRenameContract.IView> implements DeviceRenameContract.IPresent {

    private Context mContext;
    private String mDeviceName;
    private int mDeviceId;
    private Handler mHandler;
    private DeviceInfoBean mDevice;

    public DeviceRenamePresenter(Context mContext) {
        this.mContext = mContext;
        mHandler = new Handler();
    }

    @Override
    public void getDevice(String deviceName, int deviceId) {
        this.mDeviceName = deviceName;
        this.mDeviceId = deviceId;
        mDevice = DeviceDaoUtil.getInstance().findByDeviceId(mDeviceName, mDeviceId);
    }

    @Override
    public void renameGateway(String nickname) {
        CommonModelImpl mModel = new CommonModel(mHandler);
        mModel.onRenameGateway(mDevice.getIotId(), nickname, new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
                if (response.getCode() == 200){
                    mDevice.setNickName(nickname);
                    DeviceDaoUtil.getInstance().getDeviceDao().update(mDevice);
                    mHandler.post(()-> mView.onModifySuccess());
                }else {
                    mHandler.post(()-> mView.showToastMsg(response.getLocalizedMsg()));
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    @Override
    public void renameSubDevice(String nickname) {
        String ds = CoderUtils.getAscii(nickname);
        String dsCRC = ByteUtil.nameToCRC(ds);
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        SendDeviceCommand mSendEquipment = new SendDeviceCommand(mContext, deviceInfoBean);
        mSendEquipment.modifyEquipmentName(mDeviceId, ds + dsCRC);
    }

    @Override
    public void renameCamera(String nickname) {
        CameraModelImpl mModel = new CameraModel(mHandler);
        mModel.toUpdateCameraName(mDevice.getDeviceOnly(), nickname, new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
                if (response.getCode() == 200){
                    renameSuccess(nickname);
                }else {
                    mHandler.post(()-> mView.showToastMsg(response.getLocalizedMsg()));
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    @Override
    public void renameSuccess(String nickname) {
        mDevice.setSubdeviceName(nickname);
        DeviceDaoUtil.getInstance().getDeviceDao().update(mDevice);
        if (mDeviceId == -1){
            CameraDaoUtil.getInstance().updateCameraName(mDevice.getDeviceOnly(), mDevice.getDeviceName(), nickname);
        }
        mHandler.post(()-> mView.onModifySuccess());
    }
}
